Welcome back, we can have
more than one instance of a class and each one will have its own attributes,
its own internal state.
In this code, we'll create two turtles Alex and Tess.
Tess is going to draw a triangle with a thick black pen,
Alex will draw a square with a pink pen.
So let's go through the code, first we will run it.
That is Tess drawing there and now Alex is drawing.
So let's go through the code as before,
we need to import the turtle module
and create the screen that creates the place for us to do the drawings.
In this case, we get that instance bound to the variable WN and we're
calling the BG color method on it to make it be light green as you've seen here.
We create a turtle test and we set its pen size to be five,
that's what makes Tess have
this wider pen instead of just the narrow pen that's the default.
We create a second turtle called Alex and we said Alex's colored to be hot pink.
After that we have a whole segment where we
have the movement commands for Tess that's where
she does her drawing and then below that,
we have some movement commands for Alex.
Finally there's this little wn.exitonclick that just
says that when somebody clicks on the window, it disappears.
Let me make a couple of comments about the coding style
here and one thing to notice is that we
have chunked the code using some wide space and that just makes it easier to read.
That segment that I just marked lines 12 to 17,
is what causes Tess to make a triangle and
then there's a break with a blank line on line 18 and then lines 19
and 20 have her make an extra line that goes off to the left at the end of
the drawing and there's another blank space and we
get from lines 20 to 29, Alex's movements.
We don't have to do this chunking in order to make the picture come out right.
We do this chunking because it makes it easier for people
to read the programs you can sort of look at it and say oh,
there's a chunk of code from 22 to 29 and that's drawing the square.
There's a chunk of code from 12 to 17 and that's drawing a triangle.
One final thing to notice and I'm going to
rerun this so that you will get to see it again.
You might notice that on line 17,
Tess turns to the left by 120 degrees then on
line 19 she turns to the right by a 180 degrees.
We're following a little convention that lines 12 to 17 are drawing
a triangle and putting Tess back into the same orientation that she started in.
It just makes it easier for a programmer to think about it if the net impact of all
of the movements is to leave Tess back in her original position in the same orientation.
Similarly on line 29,
you'll see that after we've completed the square,
we make Alex turn to the left for 90 degrees even though we're not
going to make it move again and that's again just trying to get him
back into the same orientation he was in before.
This would be especially useful if we were going to take the whole block of
code and reuse it
and that's something that we'll see later in the course where we defined functions.
We could make a function to draw a square and wherever he starts,
the turtle would create that square and
leave the turtle in the same spot that it was in before.
So, let's go on and do one of the Parsons problems.
At the bottom of this page,
you'll find more than one Parsons problem I'm just gonna go through one of them.
Here we're going to create a program that has one turtle Jamal,
draw that capital L in blue and then Tina is going to draw on that orange line.
So this is a mixed up code problem.
We have all of the code that we need to have Jamal draw his blue L and Tina draw
her orange but your job is to drag the parts over and drop them off in the right order.
The first thing to remember about this is that,
the very first thing in any turtle program is you have to import
the turtle module so that we can have all of those turtle commands available,
and then you have to create
an instance of the screen so that there's a place for the turtles to draw.
In the instructions, it said that all the stuff of Jamal should happen first.
So those things are going to come before the Tina's stuff.
But we do have a little decision to make should we make the left and
the forward commands first or should we have this larger block first.
The clue here is that you can't have Jamal turn left before you create Jamal.
So we need to have this block first because it has a line of code
jamal= turtle.Turtle that creates the instance Jamal.
The Tina's stuff happens after Jamal.
Got to get that a little lower and finally we have
our exitonclick so that when the user clicks on the drawing it disappears.
Let's check it and make sure I got it right.
Perfect. All right, see you next time.